package com.zmn.oms.services.impl.fcorderpart;

import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.fcorderpart.OrderFactoryPartPost;
import com.zmn.oms.model.entity.fcorderpart.OrderFactoryPartPostQuery;
import com.zmn.oms.persistence.interfaces.fcorderpart.OrderFactoryPartPostDao;
import com.zmn.oms.services.interfaces.fcorderpart.OrderFactoryPartPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class OrderFactoryPartPostServiceImpl implements OrderFactoryPartPostService {

    @Autowired(required = false)
    OrderFactoryPartPostDao orderFactoryPartPostDao;

    @Override
    public Integer insert(OrderFactoryPartPost orderFactoryPartPost) {
        orderFactoryPartPost.setCreateTime(new Date());
        orderFactoryPartPost.setCreater("厂商");
        orderFactoryPartPost.setExpressTime(new Date());
        return orderFactoryPartPostDao.insertSelective(orderFactoryPartPost);
    }

    @Override
    public List<OrderFactoryPartPost> listEsOrderFactoryPartPostByOrderIdList(List<Long> orderIdList) {
        return orderFactoryPartPostDao.listEsOrderFactoryPartPostByOrderIdList(orderIdList);
    }

    @Override
    public List<OrderFactoryPartPost> listOrderFactoryPartPostByOrderId(Long orderId) {
        return orderFactoryPartPostDao.listOrderFactoryPartPostByOrderId(orderId);
    }

    @Override
    public List<OrderFactoryPartPost> listByPartId(Integer fcOrderPartId) {
        return orderFactoryPartPostDao.listByPartId(fcOrderPartId);
    }

    @Override
    public List<OrderFactoryPartPost> findByFcOrderPartId(Integer fcOrderPartId) {
        return orderFactoryPartPostDao.findByFcOrderPartId(fcOrderPartId);
    }

    @Override
    public void deleteByOrderId(Long orderId) {
        orderFactoryPartPostDao.deleteByOrderId(orderId);
    }

    @Override
    public void deleteByFcOrderPartId(Integer fcOrderPartId) {
        orderFactoryPartPostDao.deleteByFcOrderPartId(fcOrderPartId);
    }

    @Override
    public void deleteByFcOrderPartIds(List<Integer> fcOrderPartIds) {
        orderFactoryPartPostDao.deleteByFcOrderPartIds(fcOrderPartIds);
    }

    @Override
    public void deleteSend(Integer fcOrderPartId) {
        orderFactoryPartPostDao.deleteSend(fcOrderPartId);
    }

    @Override
    public void deleteReturn(Integer fcOrderPartId) {
        orderFactoryPartPostDao.deleteReturn(fcOrderPartId);
    }

    @Override
    public List<OrderFactoryPartPost> listByOrderId(Long orderId) {
        List<Long> ids = Lists.newArrayList(orderId);
        return listEsOrderFactoryPartPostByOrderIdList(ids);
    }

    @Override
    public List<OrderFactoryPartPost> listByFcPartId(Integer fcOrderPartId) {
        OrderFactoryPartPostQuery factoryPartQuery = new OrderFactoryPartPostQuery();
        factoryPartQuery.setFcOrderPartIdList(Lists.newArrayList(fcOrderPartId));
        return orderFactoryPartPostDao.listByQuery(factoryPartQuery);
    }

    @Override
    public List<OrderFactoryPartPost> listByPostIds(List<String> partPostIds) {
        return orderFactoryPartPostDao.listByPostIds(partPostIds);
    }

    @Override
    public List<OrderFactoryPartPost> listByQuery(OrderFactoryPartPostQuery factoryPartQuery){
        return orderFactoryPartPostDao.listByQuery(factoryPartQuery);
    }

    @Override
    public int findOldPartPostCount(Long orderId){
        return orderFactoryPartPostDao.findOldPartPostCount(orderId);
    }

    @Override
    public void updatePostStatus(OrderFactoryPartPost orderFactoryPartPost) {
        orderFactoryPartPostDao.updatePostStatus(orderFactoryPartPost);
    }

    @Override
    public List<OrderFactoryPartPost> listByExpressCode(String code) {
        return orderFactoryPartPostDao.listByExpressCode(code);
    }

    @Override
    public List<OrderFactoryPartPost> listForOrderPart(OrderFactoryPartPostQuery factoryPartQuery) {
        return orderFactoryPartPostDao.listForOrderPart(factoryPartQuery);
    }


    @Override
    @Transactional
    public void updateExpressById(List<OrderFactoryPartPost> orderFactoryPartPostList) throws OmsBaseException {
        for (OrderFactoryPartPost item : orderFactoryPartPostList) {
            OrderFactoryPartPost obj = new OrderFactoryPartPost();
            obj.setPostId(item.getPostId());
            obj.setPostAddressType(item.getPostAddressType());
            obj.setAddress(item.getAddress());
            obj.setConsigneeName(item.getConsigneeName());
            obj.setConsigneePhone(item.getConsigneePhone());
            obj.setStatus(item.getStatus());
            obj.setExpressData(item.getExpressData());
            obj.setExpressCode(item.getExpressCode());
            obj.setExpressTime(item.getExpressTime());
            obj.setRemark(item.getRemark());
            if (Objects.equals(item.getAssignExpressCompany(), GlobalConsts.NO)) {
                obj.setExpressCompany(item.getExpressCompany());
                obj.setExpressCompanyCode(item.getExpressCompanyCode());
            }

            orderFactoryPartPostDao.updateByPrimaryKeySelective(obj);
        }
    }

    /**
     * 根据快递单号和快递公司编码查询订单id
     * @param expressCode
     * @param expressCompanyCode
     * @return java.lang.Long
     */
    @Override
    public OrderFactoryPartPost findOrderIdByExpressCodeAndExpressCompanyCode(String expressCode, String expressCompanyCode){
        return orderFactoryPartPostDao.findOrderIdByExpressCodeAndExpressCompanyCode(expressCode, expressCompanyCode);
    }


}
